﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace CowboyAndZombies
{
    public class Cowboy
    {
        public Vector2 posicao;
        private Rectangle retangulo;
        public Texture2D textura;
        private int velocidade = 10;
        public float angulo;
        public int vida;

        public void Inicializacao(GameWindow window)
        {
            posicao.X = window.ClientBounds.Width / 2;
            posicao.Y = window.ClientBounds.Height / 2;

            vida = 100;

        }

        public void Update(GameTime gameTime, Background background, GameWindow window)
        {
            KeyboardState keyboard = Keyboard.GetState();

            if (keyboard.IsKeyDown(Keys.W))
            {
                if (posicao.Y <= 0)
                {
                    posicao.Y = 0;
                }
                else
                {
                    if ((posicao.Y >= (window.ClientBounds.Height / 2)) || (background.posicao.Y >= 0))
                    {
                        posicao.Y -= velocidade;
                    }
                }
            }

            if (keyboard.IsKeyDown(Keys.S))
            {
                if (posicao.Y == ((window.ClientBounds.Height)) - textura.Height)
                {
                    posicao.Y = ((window.ClientBounds.Height)) - textura.Height;
                }
                else
                {
                    if ((posicao.Y <= (window.ClientBounds.Height / 2)) || (background.posicao.Y <= (background.textura.Height - window.ClientBounds.Height) * -1))
                    {
                        posicao.Y += velocidade;
                    }
                }
            }

            if (keyboard.IsKeyDown(Keys.A))
            {
                if (posicao.X <= 0)
                {
                    posicao.X = 0;
                }
                else
                {
                    if ((posicao.X >= (window.ClientBounds.Width / 2)) || (background.posicao.X >= 0))
                    {
                        posicao.X -= velocidade;
                    }
                }
            }

            if (keyboard.IsKeyDown(Keys.D))
            {
                if (posicao.X >= window.ClientBounds.Width - textura.Width)
                {
                    posicao.X = window.ClientBounds.Width - textura.Width;
                }
                else
                {
                    if ((posicao.X <= (window.ClientBounds.Width / 2)) || (background.posicao.X <= (background.textura.Width - window.ClientBounds.Width) * -1))
                    {
                        posicao.X += velocidade;
                    }
                }
            }

        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Cursor cursor)
        {
            retangulo.Width = textura.Width;
            retangulo.Height = textura.Height;

            //Calcula a distância do Cowboy até o mouse
            float XDistancia = posicao.X - cursor.posicao.X;
            float YDistancia = posicao.Y - cursor.posicao.Y;

            angulo = (float)Math.Atan2(YDistancia, XDistancia);

            spriteBatch.Draw(textura, posicao, retangulo, Color.White, angulo, new Vector2(textura.Width / 2, textura.Height / 2), 1, SpriteEffects.None, 0);
        }
    }
}
